package thread.volatile_base;

/**
 * Created by mHandler on 2018/5/30 0030.
 * Description:
 */
public class RunThread2 extends Thread {

     public static int count;
    //注意一定要添加static关键字
    //这样synchronized与static锁的内容就是类对象
    //保证了同步的效果
    synchronized private static void addCount(){
        for (int i = 0; i < 100; i++){
            count++;
        }
        System.out.println("count=" + count);
    }

    @Override
    public void run() {
        addCount();
    }

    public static void main(String[] args) {
        RunThread2[] thread = new RunThread2[100];
        for (int i = 0; i < 100; i++){
            thread[i] = new RunThread2();
        }

        for (int i = 0; i < 100; i++){
            thread[i].start();
        }
    }
}
